package com.cat.config;

import com.cat.component.MyInterceptor;
import com.cat.component.MyLocaleResolver;
import org.omg.PortableServer.AdapterActivator;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.boot.autoconfigure.web.ResourceProperties;
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 自己配置功能取代springboot的自动配置
 */
@Configuration  //声明这是一个配置类
public class MyMvcConfig extends WebMvcAutoConfiguration {
    @Bean
    public WebMvcConfigurer webMvcConfigurer(){
        WebMvcConfigurer webMvcConfigurer = new WebMvcConfigurer() {
            //视图转换器 将传过来的url地址转换成相对应的url
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/").setViewName("index");
                registry.addViewController("/index").setViewName("index");
                registry.addViewController("/index.html").setViewName("index");
                registry.addViewController("/main.html").setViewName("dashboard");
            }
            //注册拦截器


            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**")
                        .excludePathPatterns("/","/index","/index.html","/user/login",
                                "/asserts/**", "/webjars/**", "/public/**", "/resources/**",
                                "/templates/**");
            }
        };
        return webMvcConfigurer;
    }
    //向容器中加载国际化配置
    @Bean
    public LocaleResolver localeResolver(){
        return new MyLocaleResolver();
    }
}
